#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8080
#define BUFFER_SIZE 1024

int main()
{
    int sock = 0;
    struct sockaddr_in serv_addr;
    char buffer[BUFFER_SIZE] = {0};
    const char *hello = "Hello from client";

    // 创建socket文件描述符
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Socket creation error \n");
        return -1;
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    // 将IP地址从字符串转换为网络字节序
    if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
    {
        printf("\nInvalid address/ Address not supported \n");
        return -1;
    }

    // 连接到服务器
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("\nConnection Failed \n");
        return -1;
    }

    // 发送数据到服务器
    send(sock, hello, strlen(hello), 0);
    printf("Hello message sent\n");

    // 读取服务器发送的数据
    read(sock, buffer, BUFFER_SIZE);
    printf("Message from server: %s\n", buffer);

    close(sock);
    return 0;
}

cpp :
#include <iostream>
#include <string>
#include <cstring>
#include <stdexcept>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

    class UDPServer
{
public:
    UDPServer(const std::string &ip, int port)
    {
        // 创建套接字
        server_fd_ = socket(AF_INET, SOCK_DGRAM, 0);
        if (server_fd_ < 0)
        {
            throw std::runtime_error("Failed to create socket");
        }

        // 设置地址重用选项
        int opt = 1;
        if (setsockopt(server_fd_, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) < 0)
        {
            close(server_fd_);
            throw std::runtime_error("Failed to set socket options");
        }

        // 绑定地址
        address_.sin_family = AF_INET;
        address_.sin_addr.s_addr = inet_addr(ip.c_str());
        address_.sin_port = htons(port);

        if (bind(server_fd_, reinterpret_cast<struct sockaddr *>(&address_), sizeof(address_)) < 0)
        {
            close(server_fd_);
            throw std::runtime_error("Failed to bind socket");
        }
    }

    ~UDPServer()
    {
        close(server_fd_);
    }

    void receive(std::string &buffer)
    {
        socklen_t addr_len = sizeof(address_);
        ssize_t bytes_received = recvfrom(server_fd_, buffer.data(), buffer.size(), 0,
                                          reinterpret_cast<struct sockaddr *>(&address_), &addr_len);
        if (bytes_received < 0)
        {
            throw std::runtime_error("Failed to receive data");
        }
        buffer.resize(bytes_received);
    }

    void send(const std::string &buffer)
    {
        ssize_t bytes_sent = sendto(server_fd_, buffer.data(), buffer.size(), 0,
                                    reinterpret_cast<const struct sockaddr *>(&address_), sizeof(address_));
        if (bytes_sent < 0)
        {
            throw std::runtime_error("Failed to send data");
        }
    }

private:
    int server_fd_;
    struct sockaddr_in address_;
};

int main()
{
    try
    {
        UDPServer server("127.0.0.1", 8080);
        std::string buffer(1024, '\0');

        std::cout << "Server started, waiting for data..." << std::endl;

        while (true)
        {
            server.receive(buffer);
            std::cout << "Received: " << buffer << std::endl;

            // 发送响应
            server.send("Message received");
        }
    }
    catch (const std::exception &e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
